#include "Camera.h"
#include "base.h"
#include "Constant.h"
#include "Types.h"
Camera::Camera(void)
{
	m_position = vector3(0.0f, 0.0f ,0.0f);
	m_orientation = vector3(0.0f, 0.0f, 0.0f);
	m_fFov = 75.0f;
	m_matProjection = matrix::Perspective(m_fFov, SCREEN_ASPECT, 0.2f, 1000.0f);
	_RecalcViewMatrix();
	_RecalcFrustum();
}
Camera::~Camera(void)
{
}
matrix Camera::GetProjection() const
{
	return m_matProjection;
}
matrix Camera::GetBillboardMatrix() const
{
	matrix rotY = matrix::RotationY(m_orientation.y);
	matrix rotZ = matrix::RotationX(m_orientation.x);
	return rotY*rotZ;
}
matrix Camera::GetViewProjectionMatrix() const
{
	return m_matProjection * m_matView;
}
void Camera::_RecalcViewMatrix()
{
	matrix trans = matrix::Translation(-m_position);
	matrix rotX = matrix::RotationX(-m_orientation.x);
	matrix rotY = matrix::RotationY(-m_orientation.y);
	matrix rotZ = matrix::RotationZ(-m_orientation.z);
	matrix rot = rotX * rotY * rotZ;
	m_matView = rot * trans;
}
void Camera::_RecalcFrustum()
{
	m_cullingFrustum = frustum(this);
}
matrix Camera::GetViewMatrix() const
{
	return m_matView;
}
vector3 Camera::MoveFacing(const vector3 &movement)
{	
	vector3 zAxis = GetDirection();
	vector3 xAxis = zAxis.Rotate(0.0f, 90.0f, 0.0f);
	vector3 yAxis = vector3(0.0f, 1.0f, 0.0f);
	xAxis = vector3(xAxis.x, 0.0f, xAxis.z);
	vector3 newPosition = m_position;
	newPosition += (zAxis * movement.z + xAxis * movement.x + yAxis * movement.y);

	_RecalcViewMatrix();
	_RecalcFrustum();
	return (m_position = newPosition);
}
vector3 Camera::GetDirection() const
{
	return vector3(0.0f,0.0f,1.0f).Normalize().Rotate(m_orientation);
}
void Camera::SetOrientation(vector3 &rot)
{ 
	m_orientation = rot;
	_RecalcViewMatrix();
	_RecalcFrustum();
}
void Camera::SetPosition(vector3 &pos)
{ 
	m_position = pos;
	_RecalcViewMatrix();
	_RecalcFrustum();
}
vector3 Camera::GetOrientation() const
{ 
	return m_orientation; 
}
vector3 Camera::GetPosition() const
{ 
	return m_position;
}